import tables
from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.dml.color import ColorFormat, RGBColor
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm, Pt
from pptx.enum.chart import XL_LEGEND_POSITION
from typing import List

prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = '报告'
from ppt.demo2 import analyse

drc_dict, lvs_dict, erc_dict, sft_dict = analyse()


def add_summary(name, dic):
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes
    shapes.title.text = " "
    chart_data = ChartData()
    chart_data.categories = list(dic.keys())
    chart_data.add_series(name, tuple([v['count'] for k, v in dic.items()]))
    x, y, cx, cy = Inches(0), Inches(0), Inches(10), Inches(7)
    slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)
    pass


def add_all_summary():
    for name, d in zip(["DRC", "LVS", "ERC", "SOFTCHK"], [drc_dict, lvs_dict, erc_dict, sft_dict]):
        add_summary(name, d)


# load csv
with open("regressionresult.csv", "r") as f:
    result_csv = f.readlines()

result_list = []
for line in result_csv:
    line = line.strip()
    line_list = line.split(",")
    result_list.append(line_list)


def iter_cell(table):
    cell_list = []
    for row in table.rows:
        for cell in row.cells:
            cell_list.append(cell)
    return cell_list


def add_slide(data: List[List]):
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes

    # shapes.title.text = '报告'
    name_objs = ["id", "block_id", "com_id", "result_type", "result_rule", "result_count"]

    # style
    rows = len(data)
    cols = len(name_objs)
    top = Cm(0.5)
    left = Cm(0.5)
    width = Cm(24)
    height = Cm(6)

    # add table to pptx
    table = shapes.add_table(rows, cols, left, top, width, height).table

    table.columns[0].width = Cm(2)
    table.columns[1].width = Cm(3)
    table.columns[2].width = Cm(7)
    table.columns[3].width = Cm(2)
    table.columns[4].width = Cm(6)
    table.columns[5].width = Cm(4)

    table.columns[0].height = Cm(2)
    table.columns[1].height = Cm(2)
    table.columns[2].height = Cm(2)
    table.columns[3].height = Cm(2)
    table.columns[4].height = Cm(2)
    table.columns[5].height = Cm(2)

    for i in range(len(name_objs)):
        table.cell(0, i).text = name_objs[i]

    for i in range(1, rows):
        for line in data:
            for index, w in enumerate(line):
                table.cell(i, index).text = str(w)

    for cell in iter_cell(table):
        for paragraph in cell.text_frame.paragraphs:
            for run in paragraph.runs:
                # run.font.color.rgb =RGBColor(100, 34, 56)
                run.font.size = Pt(8)

    prs.save("test.pptx")


if __name__ == '__main__':
    size = 19
    add_all_summary()
    for i in range(0, len(result_list), size):
        data = result_list[i:i + size]
        add_slide(data)
